package de.distmlp.preprocessing.rawData;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * The parser are expecting comma separated values while the raw data extracted
 * from xing looks like this:
 * 
 * Heinz_Ettel::Unternehmer-Beratung;China Biz;Zugang zum chinesischen
 * Markt;Projektleitung in China;Informationen ueber den Markt in China;China
 * Sources;international Marketing;Firmeninvestments;Joint
 * Ventures;Export;Environment Protection ;Environment Protection
 * Technology;Environmental Knowhow;nachhaltige Loesungen;Solar
 * Module;PV;erneuerbare Energien;renewable
 * energy;Projektmanagement;Coaching;Customer Relationship
 * Management;CRM;Management Information Systeme;MIS;deut sche Umwelttechnik für
 * China;为 -;green technology;sustainable solutions
 * 
 * @author twist
 * 
 */
public class RawDataConverter {

	private static final String OUTPUTFILE_EXTENSION = ".pre";

	public static void parseAndWrite(final String inputFileName) throws IOException {
		final File inputFile = new File(inputFileName);

		if (inputFile.exists()) {
			final String outputFileName = RawDataConverter.getOutputFileName(inputFileName);

			final File outputFile = new File(outputFileName);
			if (outputFile.exists()) {
				outputFile.delete();
			}

			final BufferedReader reader = new BufferedReader(new FileReader(inputFile));
			final BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
			String line;

			while ((line = reader.readLine()) != null) {
				if (RawDataConverter.skipLine(line)) {
					continue;
				}
				final int colonIndex = line.indexOf("::") + 2;
				final StringBuilder tmpLine = new StringBuilder();
				final String strippedInput = line.substring(colonIndex, line.length()).replace(",", "").replace("..", "")
						.replace("...", "").replace("....", "").replace("(", "").replace(")", "").replace("--", "").replace("-", "")
						.replace(".", "").replace("etc", "").replace("mit", "").replace(" und ", "").replace(" ", "");
				final String[] array = strippedInput.split(";");
				int featureSize = 0;
				int counter = 0;
				for (final String element : array) {
					if ((element.trim().length() < 15) && (element.trim().length() > 1)) {
						tmpLine.append(element.trim().toLowerCase());
						featureSize++;
						if (counter != (array.length - 1)) {
							tmpLine.append(',');
						}
					}
					counter++;
				}
				if (featureSize > 2) {
					if (tmpLine.toString().endsWith(",")) {
						writer.write(tmpLine.toString().substring(0, tmpLine.toString().length() - 1));
					} else {
						writer.write(tmpLine.toString());
					}

					writer.newLine();
				}
			}
			reader.close();
			writer.flush();
			writer.close();
		} else {
			System.out.println("File " + inputFileName + " does not exist!");
		}
	}

	public static void main(final String[] args) throws IOException {
		RawDataConverter.parseAndWrite("/home/twist/Dropbox/htw/master/master thesis_share/Xing Daten vom 05.09.2013/xingOutput.txt");
	}

	private static boolean skipLine(final String line) {
		final int colonIndex = line.indexOf("::");
		if (colonIndex == -1) {
			return true;
		}
		final int semicolonIndex = line.indexOf(";");
		if (semicolonIndex == -1) {
			return true;
		}
		return false;
	}

	private static String getOutputFileName(final String inputFileName) {
		String outputFileName = inputFileName;

		final int dotIndex = inputFileName.lastIndexOf(".");
		if (dotIndex > 0) {
			outputFileName = inputFileName.substring(0, dotIndex) + RawDataConverter.OUTPUTFILE_EXTENSION;
		} else {
			outputFileName = inputFileName + RawDataConverter.OUTPUTFILE_EXTENSION;
		}
		return outputFileName;
	}
}
